Function.RegisterNamespace("System.IO.FileStrategy.V8");

System.IO.FileStrategy.V8.Posix=function(){
    var _bufferSize=100000;

    // IFileStrategy Members
    this.DeleteFile=function(path){
        if(this.Exists(path)){
            return System.Environment.Execute("rm",["-f",path]);
        }
        return false;
    };
    
    this.Exists=function(path){
        return System.Environment.Execute("sh", ["-c",String.Format("test -f \"{0}\" && echo 1 || echo 0",path)])==1;
    };

    this.GetFile=function(path){
        if(this.Exists(path)){
            return read(path);
        }
        return null;
    };
        
    this.SaveFile=function(path,text){
        this.DeleteFile(path);
        if(text==undefined||!text.length){
            return System.Environment.Execute("touch",[path],true);
        }
        var chunks=Math.ceil(text.length/_bufferSize);
        for(var i=0;i<chunks;i++){
            var index=i*_bufferSize;
            System.Environment.Execute("sh", ["-c",String.Format("cat >> \"{0}\" <<'_EOF_'\n{1}\n_EOF_",path,text.slice(index,index+_bufferSize))]);
            System.Environment.Execute("perl", ["-pi","-e","chomp if eof",path]);
        }
    };

    // IStrategySpecification Members
    this.IsSatisfiedBy=function(candidate){
        if(typeof(os)!='undefined' && typeof(os.system)!='undefined'){
            try{
                os.system("uname");
                return true;
            }catch(e){}
        }
        return false;
    };
};

System.IO.FileStrategy.V8.Posix.Implement(System.IO.FileStrategy.IFileStrategy,'System.IO.FileStrategy.V8.Posix');
System.IO.FileStrategy.V8.Posix.Implement(System.Script.Strategy.IStrategySpecification,'System.IO.FileStrategy.V8.Posix');

System.IO.File.Strategies.Add(System.IO.FileStrategy.V8.Posix);